﻿//**************************************************************************//
// Copyright (C) Abdul Rasheed. All rights Reserved.                        //
// rasheedat.blogspot.com                                                   //
//**************************************************************************//
using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PPHR.Common;
using PPHR.DataLogic;
namespace PPHR
{
    public partial class VisitList : Form
    {
        public VisitList()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Close the form and show the previous window
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItemClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Go to main screen
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItemHome_Click(object sender, EventArgs e)
        {
            this.Hide();
            PPHR phr = new PPHR();
            phr.Show();
        }

        /// <summary>
        /// Open visit detail screen to add new visit information
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItemNew_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            try
            {
                ShowVisitDetail(Constants.ACTION_NEW);
                Cursor.Current = Cursors.Default;
            }
            catch (Exception ex)
            {
                Cursor.Current = Cursors.Default;
                MessageBox.Show(ex.Message, Constants.MessageBoxCaption);
            }

        }

        /// <summary>
        /// Open Visit Detail form with selected visit id
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItemEdit_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            try
            {
                ShowVisitDetail(Constants.ACTION_EDIT);
                Cursor.Current = Cursors.Default;
            }
            catch (Exception ex)
            {
                Cursor.Current = Cursors.Default;
                MessageBox.Show(ex.Message, Constants.MessageBoxCaption);
            }
        }

        /// <summary>
        /// Load visit detail form with visit details for delete
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItemDelete_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            try
            {
                ShowVisitDetail(Constants.ACTION_DELETE);
                Cursor.Current = Cursors.Default;
            }
            catch (Exception ex)
            {
                Cursor.Current = Cursors.Default;
                MessageBox.Show(ex.Message, Constants.MessageBoxCaption);
            }
        }

        /// <summary>
        /// Load visit detail form with visit details for view
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItemView_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            try
            {
                ShowVisitDetail(Constants.ACTION_VIEW);
                Cursor.Current = Cursors.Default;
            }
            catch (Exception ex)
            {
                Cursor.Current = Cursors.Default;
                MessageBox.Show(ex.Message, Constants.MessageBoxCaption);
            }
        }

        /// <summary>
        /// Based on action show the visit detail form
        /// </summary>
        /// <param name="Action"></param>
        private void ShowVisitDetail(int Action)
        {
            int visitID = -1;
            if (Action != Constants.ACTION_NEW)
            {
                DataTable dtVisits = (DataTable)dgVisitList.DataSource;
                int index = dgVisitList.CurrentRowIndex;
                DataRow dr = dtVisits.Rows[index];
                visitID = int.Parse(dr[1].ToString());
            }
            VisitDetail PV = new VisitDetail(Action, visitID);
            PV.Show();
        }

        private void VisitList_Load(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            LoadVisitDetail();
            Cursor.Current = Cursors.Default;
        }

        /// <summary>
        /// Refresh the visit list on focus
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void VisitList_GotFocus(object sender, EventArgs e)
        {
            LoadVisitDetail();
        }

        /// <summary>
        /// Bind the results in datagrid
        /// </summary>
        private void LoadVisitDetail()
        {
            VisitDataLogic visits = new VisitDataLogic();
            DataTable dtVisits = visits.VisitList();
            dgVisitList.DataSource = dtVisits;
        }

        /// <summary>
        /// If there is no records in grid then disable edit and delete menu
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void contextMenuAction_Popup(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = (DataTable)dgVisitList.DataSource;
                if (dt.Rows.Count <= 0)
                {
                    contextMenuAction.MenuItems[1].Enabled = false;
                    contextMenuAction.MenuItems[2].Enabled = false;
                    contextMenuAction.MenuItems[3].Enabled = false;
                }
                else
                {
                    contextMenuAction.MenuItems[1].Enabled = true;
                    contextMenuAction.MenuItems[2].Enabled = true;
                    contextMenuAction.MenuItems[3].Enabled = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Constants.MessageBoxCaption, MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
            }
        }

    }
}